package com.hr.reactive.mono.s01;

import com.hr.reactive.mono.s01.pub.PublisherImpl;
import com.hr.reactive.mono.s01.sub.SubscriberImpl;

import java.util.concurrent.TimeUnit;

public class Demo {
    public static void main(String[] args) throws InterruptedException {
        demo04();
    }

    private static void demo01() {
        var pub = new PublisherImpl();
        var sub = new SubscriberImpl();
        pub.subscribe(sub);
    }

    private static void demo02() throws InterruptedException {
        var pub = new PublisherImpl();
        var sub = new SubscriberImpl();
        pub.subscribe(sub);
        sub.getSubscription().request(3);
        TimeUnit.SECONDS.sleep(2);
        sub.getSubscription().request(3);
        TimeUnit.SECONDS.sleep(2);
        sub.getSubscription().request(3);
        TimeUnit.SECONDS.sleep(2);
    }
    private static void demo03() throws InterruptedException {
        var pub = new PublisherImpl();
        var sub = new SubscriberImpl();
        pub.subscribe(sub);
        sub.getSubscription().request(3);
        TimeUnit.SECONDS.sleep(2);
        sub.getSubscription().cancel();
        System.out.println("cancel");
        sub.getSubscription().request(3);
        TimeUnit.SECONDS.sleep(2);
        sub.getSubscription().request(3);
    }
    private static void demo04() throws InterruptedException {
        var pub = new PublisherImpl();
        var sub = new SubscriberImpl();
        pub.subscribe(sub);
        sub.getSubscription().request(3);
        TimeUnit.SECONDS.sleep(2);
        sub.getSubscription().request(30);
        TimeUnit.SECONDS.sleep(2);
        sub.getSubscription().request(3);
    }
}
